<!--
 * @Author: huangyq
 * @Date: 2022-04-22 10:40:22
 * @LastEditTime: 2023-12-06 09:57:52
 * @LastEditors: hyq
 * @Description: 面包屑
 * @FilePath: /vue3-boilerplate-pc-client/src/views/pages/components/hq-breadcrumb/index.vue
-->
<template>
  <div class="hq-breadcrumb">
    <div class="hq-breadcrumb_left">
      <el-icon class="hq-breadcrumb_left--icon" @click="handleCollapse">
        <fold v-if="!isCollapse" />
        <expand v-else />
      </el-icon>
      <transition name="fade">
        <el-breadcrumb separator-class="el-icon-arrow-right">
          <el-breadcrumb-item to="/dashboard"> 主页 </el-breadcrumb-item>
          <el-breadcrumb-item
            v-for="item in filterMatched || []"
            :key="item.name"
          >
            {{ item.meta.title }}
          </el-breadcrumb-item>
        </el-breadcrumb>
      </transition>
    </div>
    <div class="hq-breadcrumb_right">
      <el-badge :value="warning" class="item">
        <el-icon><bell-filled /></el-icon>
      </el-badge>
      <el-dropdown>
        <span class="el-dropdown-link">
          {{ userStore.name }}
          <el-icon class="el-icon--right">
            <arrow-down />
          </el-icon>
        </span>
        <template #dropdown>
          <el-dropdown-menu>
            <el-dropdown-item @click="logout"> 退出登录 </el-dropdown-item>
          </el-dropdown-menu>
        </template>
      </el-dropdown>
    </div>
  </div>
</template>

<script setup lang="ts">
import { onBeforeMount, ref, computed } from 'vue'
import { useRoute } from 'vue-router'
import { ElMessage, ElMessageBox } from 'element-plus'
import { LoginApi } from '@/apis'
import { clearStorage } from '@/utils'
import { ArrowDown, BellFilled, Expand, Fold } from '@element-plus/icons-vue'
import { useUserStore } from '@/stores'

const route = useRoute()
const filterMatched = computed(() => {
  return route.matched.slice(1)
})

// 菜单切换
const isCollapse = ref<boolean>(false)
const emit = defineEmits(['onCollapse'])
function handleCollapse() {
  isCollapse.value = !isCollapse.value
  emit('onCollapse', isCollapse.value)
}

// 获取用户信息
const userStore = useUserStore()
onBeforeMount(() => {
  userStore.fetchUserInfo()
})

// 获取告警消息
const warning = ref<string>('')
onBeforeMount(() => {
  warning.value = ''
})

// 退出登录
const logout = () => {
  ElMessageBox.confirm('是否退出登录？', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  })
    .then(async () => {
      const res = await LoginApi.logoutUsingPOST({})
      if (res.data.success) {
        ElMessage.success('退出登录成功')
        clearStorage()
        setTimeout(() => {
          location.reload()
        }, 1500)
      }
    })
    .catch(() => {
      return false
    })
}
</script>

<style lang="scss">
.hq-breadcrumb {
  width: 100%;
  height: 46px;
  flex-grow: 0;
  flex-shrink: 0;
  padding: 0 10px;
  display: flex;
  align-items: center;
  border-bottom: 2px solid #efefef;
  justify-content: space-between;
  background-color: #fff;
  &_left {
    flex: 1;
    display: flex;
    align-items: center;
    &--icon {
      margin-right: 10px;
      font-size: 22px;
    }
  }
  &_right {
    width: 200px;
    display: flex;
    justify-content: flex-end;
    align-items: center;
    font-size: 20px;
    flex-grow: 0;
    flex-shrink: 0;
  }
  .el-badge {
    display: flex;
    align-items: center;
    margin-right: 20px;
  }
}
</style>
